package com.cdtu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdtu.entity.SysContract;
import com.cdtu.entity.SysDept;
import com.cdtu.entity.SysEmployee;
import com.cdtu.service.SysContractService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/contractController")
public class ContractControllerGzl {

    @Resource
    private SysContractService sysContractService;

    @RequestMapping("/list")
    public String list( Model model) {
        List<SysContract> sysContractList = sysContractService.list();
        model.addAttribute("sysContractList",sysContractList);
        return "/system/contract/contractList";
    }

    @RequestMapping("/addContract")
    public String addSysEmloyee( SysContract sysContract,String signedTimeStr,Model model) throws ParseException {
        Date date = new SimpleDateFormat("yy-MM-dd'T'HH:mm").parse(signedTimeStr);
        sysContract.setSignedTime(date);
        boolean save = sysContractService.save(sysContract);
        return "redirect:/contractController/list";
    }

    //删除一个
    @RequestMapping("delete")
    public String deleteSysEmloyee(SysContract sysContract,HttpServletRequest request){
        Long employeeId = Long.valueOf(sysContract.getId());
         boolean m =   sysContractService.removeById(employeeId);

       /* String[] ids = request.getParameterValues("id");
        System.out.println(ids);
        for (SysContract contract : sysContractList) {
            System.out.println(contract.getId());
        }*/
        return "redirect:/contractController/list";
    }
    //批量删除
    @RequestMapping("deleteMulti")
    public String deleteManey(SysContract sysContract,HttpServletRequest request){
        String[] idStr = request.getParameterValues("ids");
        System.out.println("=================================="+idStr);
        int[] id = new int[idStr.length];
        for (int i = 0; i < idStr.length; i++) {
            id[i] = Integer.parseInt(idStr[i]);
            boolean m =   sysContractService.removeById( id[i]);
        }
        return "redirect:/contractController/list";
    }

    @RequestMapping("/updateByIdShow")
    public String updateByIdShow(SysContract sysContract1,Model model) {
        Long employeeId = Long.valueOf(sysContract1.getId());
         SysContract sysContract = sysContractService.getById(employeeId);
        model.addAttribute("sysContract",sysContract);
        return "/system/contract/updateContract";
    }


    @RequestMapping("/update")
    public String updateById(SysContract sysContract,String signedTimeStr) throws ParseException {
        Date date = new SimpleDateFormat("yy-MM-dd'T'HH:mm").parse(signedTimeStr);
        sysContract.setSignedTime(date);
        boolean b = sysContractService.updateById(sysContract);
        return "redirect:/contractController/list";
    }

    @RequestMapping("/like")
    public String likeQuery(Model model, String nameStr)
    {
        System.out.println(nameStr);
        List<SysContract> sysContractList = sysContractService.list(new QueryWrapper<SysContract>().like("name",nameStr));
        model.addAttribute("sysContractList",sysContractList);
        for (SysContract sysContract : sysContractList) {
            System.out.println(sysContract);
        }
      /*  List<SysContract> sysContractList = sysContractService.list();
        model.addAttribute("sysContractList",sysContractList);*/
        return "/system/contract/contractList";

    }

}
